package com.bawei.contentprovider;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

/**
 * @Author : yaotianxue
 * @Time : On 2023/6/6 14:46
 * @Description : MyProvider
 */
public class MyProvider extends ContentProvider {
    private MyHelper myHelper;
    private SQLiteDatabase db;
    //获取数据库 返回值创建成功或失败
    @Override
    public boolean onCreate() {
        Log.d("ytx", "ContentProvider onCreate: ");
        myHelper = new MyHelper(getContext());
        db = myHelper.getWritableDatabase();
        if( db== null){
            return false;
        }else{
            return true;
        }

    }
    //查询数据
    @Nullable
    @Override
    public Cursor query(@NonNull Uri uri, @Nullable String[] projection, @Nullable String selection, @Nullable String[] selectionArgs, @Nullable String sortOrder) {
        return db.query("user",projection,selection,selectionArgs,null,null,sortOrder);
    }

    @Nullable
    @Override
    public String getType(@NonNull Uri uri) {
        return null;
    }

    @Nullable
    @Override
    public Uri insert(@NonNull Uri uri, @Nullable ContentValues values) {
        db.insert("user",null,values);
        return uri;
    }

    @Override
    public int delete(@NonNull Uri uri, @Nullable String selection, @Nullable String[] selectionArgs) {
        int num = db.delete("user", selection, selectionArgs);
        return num;
    }

    @Override
    public int update(@NonNull Uri uri, @Nullable ContentValues values, @Nullable String selection, @Nullable String[] selectionArgs) {
        return db.update("user",values,selection,selectionArgs);
    }

    @Nullable
    @Override
    public Bundle call(@NonNull String method, @Nullable String arg, @Nullable Bundle extras) {
        return super.call(method, arg, extras);
    }
}
